/**
  * This file is part of Medusa.
  *
  * Authors: Uriel Fanelli <uriel.fanelli@gmail.com>
  *          Perthect <perthect@gmail.com>
  *          Marco Bizzarri <Marco.Bizzarri@gmail.com>
  *
  *
  * Medusa is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.

  * Medusa is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.

  * You should have received a copy of the GNU General Public License
  * along with Medusa; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */


#include "tabwidget.h"
#include "singletab.h"

tabWidget::tabWidget(GProgressBar *finestra,QTAstbar *x)
{
    progressBar=finestra;
    qsb=x;
    setMovable(true);
    setTabsClosable(true);
    connect(this,SIGNAL(tabCloseRequested(int)),this,SLOT(chiuditab(int)));
  //TAB|PUFF|RELOAD (NEVER SEEN BEFORE)
  //connect(this,SIGNAL(currentChanged(int)),this,SLOT(aggiornatab(int))); // MrsUriel ringrazia, io no!
    this->manager = new QNetworkAccessManager;
    singleTab *stab=new singleTab(this->manager);
}

void tabWidget::createTabForURL(QUrl url, QIcon icon, QString label)
{
    singleTab *stab = new singleTab(this->manager);
    stab->mostrapagina(url);
    addTab(stab, icon, label);
    setTabToolTip(count() - 1, url.toString());
    setCurrentIndex(count() - 1);
    connect(stab->view, SIGNAL(urlChanged(QUrl)), this, SLOT(aggiornatabtip(QUrl)));
    qsb->setViewName(url.toString());
    connect(stab->page, SIGNAL(linkClicked(QUrl)),  qsb, SLOT(setViewNameU(QUrl))); // DELEGATION MISSING
    connect(stab->view, SIGNAL(loadStarted()),      qsb, SLOT(pageLoadStarted()));
    connect(stab->view, SIGNAL(loadProgress(int)),  qsb, SLOT(pageLoadInProgress(int)));
    connect(stab->view, SIGNAL(loadFinished(bool)), qsb, SLOT(pageLoadFinished(bool)));
}

bool tabWidget::noTabsOn()
{
    return count() < 1;
}

int tabWidget::findMatchingTab(QUrl url)
{
    for (int i = 0; i < count(); ++i) {
        QWidget *widget = this->widget(i);
        if (singleTab *tab = qobject_cast<singleTab*>(widget)){
            if(tab->cometichiami() == url)
                return i;
         }
    }
    return count();
}

// aggiungitab ONLY IF NOT ON ALREADY
void tabWidget::aggiungitab(QUrl url,QIcon icon,QString label)
{
// NO TABS ON
    if (noTabsOn()) {
        createTabForURL(url, icon, label);
    } else {
// PARSE ALL TABS FOR MATCH
        int tabIndex = findMatchingTab(url);
        if (tabIndex == count()) {
            createTabForURL(url, icon, label);
        } else {
            setCurrentIndex(tabIndex);
        }
    }
}

void tabWidget::aggiornatab(int index)
{
    //if(index = -8888) // PERTHECT
    index=currentIndex();
    QWidget *widget = this->widget(index);
    if (singleTab *tab = qobject_cast<singleTab*>(widget)){
        tab->view->reload();
        qsb->setViewName((tab->view->url()).toString());
    }
}

void tabWidget::aggiornatabtip(QUrl url)
{
    setTabToolTip(currentIndex(),url.toString());
}

void tabWidget::chiuditab(int tab)
{
   removeTab(tab);
}
